import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class FileGrader {

   private File fileToGrade;
   private List<File> inputFiles;
   private List<String> expectedOutput;

   public FileGrader(File fileToGrade, List<File> inputFiles, List<String> expectedOutput) {
      this.fileToGrade = fileToGrade;
      this.inputFiles = inputFiles;
      this.expectedOutput = expectedOutput;
   }

   public int getTotal() {
      return inputFiles.size();
   }

   public int getCorrect() {
      int correct = 0;
      for (int i = 0; i < inputFiles.size(); i++) {
         try {
            String result = executeFile(fileToGrade, inputFiles.get(i));
            if (result.equals(expectedOutput.get(i).trim())) {
               correct++;
            }
         } catch (IOException e) {
            continue;
         }
      }
      return correct;
   }

   public static String executeFile(File toExecute, File inputFile) throws IOException {
      String command = (toExecute.getName().endsWith("jar") ? "java -jar " : "") + toExecute.getAbsolutePath();
      ProcessBuilder proc = new ProcessBuilder(command, inputFile.getAbsolutePath());
      Process p = proc.start();
      Scanner stdout = new Scanner(p.getInputStream());
      String result = "";
      while (stdout.hasNextLine()) {
         result += stdout.nextLine();
      }
      return result.trim();
   }
}
